﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIDataGridTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetRowOrColumnMajor()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.IsTrue(dataGrid.RowOrColumnMajor.HasValue);
            Assert.AreEqual(RowOrColumnMajor.RowMajor, dataGrid.RowOrColumnMajor.GetValueOrDefault());
        }

        [TestMethod]
        public void ShouldGetRowCount()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(6, dataGrid.RowCount);
        }

        [TestMethod]
        public void ShouldGetColumnCount()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(4, dataGrid.ColumnCount);
        }

        [TestMethod]
        public void ShouldGetItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            AutomationElement dataItem = dataGrid.GetItem(0, 0);
            Assert.IsNotNull(dataItem);

            dataItem = dataGrid.GetItem(5, 3);
            Assert.IsNotNull(dataItem);
        }

        [TestMethod]
        public void ShouldGetColumnHeaders()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            AutomationElement[] colHeaders = dataGrid.GetColumnHeaders();
            Assert.AreEqual(4, colHeaders.Length);
        }

        [TestMethod]
        public void ShouldGetRowHeaders()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            AutomationElement[] rowHeaders = dataGrid.GetRowHeaders();
            Assert.AreEqual(6, rowHeaders.Length);
        }

        [TestMethod]
        public void ShouldGetSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            var selection = dataGrid.GetSelection(); 
            Assert.AreEqual(0, selection.Length);

            dataGrid.UIDataItem().Select();
            selection = dataGrid.GetSelection();
            Assert.AreEqual(1, selection.Length);
        }

        [TestMethod]
        public void ShouldGetCanSelectMultiple()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.IsTrue(dataGrid.CanSelectMultiple);
        }

        [TestMethod]
        public void ShouldGetIsSelectionRequired()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.IsFalse(dataGrid.IsSelectionRequired);
        }

        [TestMethod]
        public void ShouldGetHorizontallyScrollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.IsFalse(dataGrid.HorizontallyScrollable);
        }

        [TestMethod]
        public void ShouldGetHorizontalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(ScrollPattern.NoScroll, dataGrid.HorizontalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetHorizontalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(100, dataGrid.HorizontalViewSize);
        }

        [TestMethod]
        public void ShouldGetVerticallyScrollable()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.IsTrue(dataGrid.VerticallyScrollable);
        }

        [TestMethod]
        public void ShouldGetVerticalScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetVerticalViewSize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(50, dataGrid.VerticalViewSize);
        }

        [TestMethod]
        public void ShouldScroll()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
            dataGrid.Scroll(ScrollAmount.NoAmount, ScrollAmount.LargeIncrement);
            Assert.AreEqual(100, dataGrid.VerticalScrollPercent);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldThrowExceptionOnScrollHorizontallyForUnsupportedDataGrid()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            dataGrid.ScrollHorizontally(ScrollAmount.LargeIncrement);
        }

        [TestMethod]
        public void ShouldScrollVertically()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
            dataGrid.ScrollVertically(ScrollAmount.LargeIncrement); 
            Assert.AreEqual(100, dataGrid.VerticalScrollPercent);
        }

        [TestMethod]
        public void ShouldSetScrollPercent()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
            dataGrid.SetScrollPercent(ScrollPattern.NoScroll, 100);
            Assert.AreEqual(100, dataGrid.VerticalScrollPercent);
        }

        [TestMethod]
        public void ShouldScrollToTop()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataGrid dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'");
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
            dataGrid.SetScrollPercent(ScrollPattern.NoScroll, 100);
            dataGrid.ScrollToTop();
            Assert.AreEqual(0, dataGrid.VerticalScrollPercent);
        }

        [TestMethod]
        public void ShouldGetAllDataGrids()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIDataGrid> dataGrids = window.UIDataGrids();
            Assert.AreEqual(1, dataGrids.Count);
            Assert.AreEqual("dataGrid1", dataGrids[0].AutomationId);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}